package com.yanqu.road.dao.impl.channelcenter.luckydraw;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.luckydraw.LuckyDrawUserData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class LuckyDrawUserDataDaoImpl extends TempDataObjectDao<LuckyDrawUserData> {

    public static String getLuckyDrawUserDataSql = "select * from `t_n_lucky_draw` where `activityId`=?";

    public Map<Long, LuckyDrawUserData> getLuckyDrawUserDataMap(int activityId) {
        Map<Long, LuckyDrawUserData> map = new ConcurrentHashMap<>();

        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getChannelServerDb());
            call = getDb().getConnection().prepareCall(getLuckyDrawUserDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                LuckyDrawUserData data = parse(rs);
                map.put(data.getUserId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }

        return map;
    }

    public static String updateLuckyDrawUserDataSql = "update `t_n_lucky_draw` set resetTime=?,canDrawTimes=?,dayDrawTimes=?,washeDayWinTimes=? where activityId=? and userId=?";

    @Override
    protected boolean updateData(LuckyDrawUserData luckyDrawUserData) {
        luckyDrawUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getChannelServerDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, luckyDrawUserData.getResetTime()));
        para.put(i++, new DbParameter(Types.INTEGER, luckyDrawUserData.getCanDrawTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, luckyDrawUserData.getDayDrawTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, luckyDrawUserData.getWasheDayWinTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, luckyDrawUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, luckyDrawUserData.getUserId()));
        if (getDb().executeNoneQuery(updateLuckyDrawUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertLuckyDrawUserDataSql = "insert into `t_n_lucky_draw`(activityId,userId,resetTime,canDrawTimes,dayDrawTimes,washeDayWinTimes) values (?,?,?,?,?,?)";

    @Override
    protected boolean addData(LuckyDrawUserData luckyDrawUserData) {
        luckyDrawUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getChannelServerDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, luckyDrawUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, luckyDrawUserData.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, luckyDrawUserData.getResetTime()));
        para.put(i++, new DbParameter(Types.INTEGER, luckyDrawUserData.getCanDrawTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, luckyDrawUserData.getDayDrawTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, luckyDrawUserData.getWasheDayWinTimes()));
        if (getDb().executeNoneQuery(insertLuckyDrawUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected LuckyDrawUserData parseData(ResultSet rs) throws SQLException {
        LuckyDrawUserData luckyDrawUserData = new LuckyDrawUserData();
        luckyDrawUserData.setActivityId(rs.getInt("activityId"));
        luckyDrawUserData.setUserId(rs.getLong("userId"));
        luckyDrawUserData.setResetTime(rs.getLong("resetTime"));
        luckyDrawUserData.setCanDrawTimes(rs.getInt("canDrawTimes"));
        luckyDrawUserData.setDayDrawTimes(rs.getInt("dayDrawTimes"));
        luckyDrawUserData.setWasheDayWinTimes(rs.getInt("washeDayWinTimes"));
        return luckyDrawUserData;
    }

}
